package city.spring.modules.medicalstation.doctorsOrderRecord.controller;

import city.spring.modules.medicalstation.doctorsOrderRecord.entity.DoctorsOrderRecordEntity;
import city.spring.modules.medicalstation.doctorsOrderRecord.service.DoctorsOrderRecordService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：医嘱记录
 *
 * @author 谢金婷
 * @date 2020-11-09 17:04:43
 */
@RestController
@RequestMapping("/doctors_order_record")
public class DoctorsOrderRecordController extends ApiController {
    private final DoctorsOrderRecordService doctorsOrderRecordService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DoctorsOrderRecordEntity, ?>> entityOrderFields;

    public DoctorsOrderRecordController(DoctorsOrderRecordService doctorsOrderRecordService) {
        this.doctorsOrderRecordService = doctorsOrderRecordService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DoctorsOrderRecordEntity::getId);
        entityOrderFields.add(DoctorsOrderRecordEntity::getOldmanId);
        entityOrderFields.add(DoctorsOrderRecordEntity::getOrderDate);
    }

    /**
     * 获取全部的 <strong>医嘱记录</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DoctorsOrderRecordEntity entity) {
        LambdaQueryChainWrapper<DoctorsOrderRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>医嘱记录</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, DoctorsOrderRecordEntity entity) {
        LambdaQueryChainWrapper<DoctorsOrderRecordEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<DoctorsOrderRecordEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>医嘱记录</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(doctorsOrderRecordService.getById(id));
    }

    /**
     * 添加一个 <strong>医嘱记录</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DoctorsOrderRecordEntity entity) {
        doctorsOrderRecordService.saveDoctorsOrderRecord(entity);
        return success(entity);

//        HttpHeaders headers = new HttpHeaders();
//        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
//        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>医嘱记录</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DoctorsOrderRecordEntity entity) {
        entity.setId(id);
        doctorsOrderRecordService.updateDoctorsOrderRecord(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>医嘱记录</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        doctorsOrderRecordService.deleteDoctorsOrderRecord(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>医嘱记录</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        doctorsOrderRecordService.deleteDoctorsOrderRecord(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DoctorsOrderRecordEntity> buildLambdaQuery(DoctorsOrderRecordEntity entity) {
        LambdaQueryChainWrapper<DoctorsOrderRecordEntity> lambdaQuery = doctorsOrderRecordService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DoctorsOrderRecordEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldmanId()), DoctorsOrderRecordEntity::getOldmanId, entity.getOldmanId());
        lambdaQuery.eq(entity.getOrderDate() != null, DoctorsOrderRecordEntity::getOrderDate, entity.getOrderDate());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DoctorsOrderRecordEntity> buildQuery(DoctorsOrderRecordEntity entity) {
        QueryWrapper<DoctorsOrderRecordEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanId()), "oldman_id", entity.getOldmanId());
        wrapper.eq(entity.getOrderDate() != null, "order_date", entity.getOrderDate());
        return wrapper;
    }

}
